package com.rd.p2p.mobile.user;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpSession;

import org.apache.struts2.convention.annotation.Action;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.checkrule.LoginRuleCheck;
import com.qdlc.p2p.dal.common.RuleCheckHelper;
import com.qdlc.p2p.dal.dto.User;

/**
 * 用户账户
 *
 * @author wuhuan
 */
public class MobileUserAction extends BaseAction<User> implements ModelDriven<User> {
    
    private static final String MOBILE_LOGIN_FAIL_COUNT = "mobile_login_fail_count";
   
	/** 登录规则 **/
	private LoginRuleCheck loginRuleCheck = RuleCheckHelper.getValue(LoginRuleCheck.class);

	@Action("/mobile/user/register")
	public String register() throws Exception {
		User u = getSessionUser();
		if (u != null) {
			redirect("/mobile/usercenter/main.html");
		}
		return "register";
	}

	/**
	 * <p>登录</p>
	 * @author 原系统 2016年1月11日 下午1:49:05
	 * @return
	 * @throws Exception
	 * @modificationHistory=========================逻辑或功能性重大变更记录
	 * @modify by user: {Kaiju Zhao} 2016年1月11日 下午1:49:05
	 * @modify by reason:{方法名}:{从session中获取错误登录次数}
	 */
	@Action("/mobile/user/login")
	public String login() throws Exception {
		User u = getSessionUser();
		if (u != null) {
			redirect("/mobile/usercenter/main.html");
		}
//		request.setAttribute("count", getFailTimes() >= loginRuleCheck.max_fail_times_code);
		// 从session中获取错误登录次数
		request.setAttribute("count", getFailTimesFromSession() >= loginRuleCheck.max_fail_times_code);
		request.setAttribute("redirectURL", request.getParameter("redirectURL"));
		return "mobile_login";
	}

	@Action("/mobile/user/getpwd")
	public String getpwd() throws Exception {
		return "getpwd";
	}

	private int getFailTimes() {
		int count = 0;
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				String name = cookie.getName();
				if ("mobile_login_fail_count".equals(name)) {
					String value = cookie.getValue();
					if (value != null) {
						count = Integer.parseInt(value);
					}
					break;
				}
			}
		}
		return count;
	}
	
	/**
     * <p>从session中获取错误登录次数</p>
     * @author Kaiju Zhao 2016年1月11日 下午12:00:58
     * @param addCount
     * @return
     * @modificationHistory=========================逻辑或功能性重大变更记录
     * @modify by user: {修改人} 2016年1月11日 下午12:00:58
     * @modify by reason:{方法名}:{原因}
     */
    private int getFailTimesFromSession() {
        int count = 0;
        HttpSession httpSession = request.getSession(); 
        if (null != httpSession) {
            Object countObj = httpSession.getAttribute(MOBILE_LOGIN_FAIL_COUNT);
            if (null != countObj) {
                String countStr = httpSession.getAttribute(MOBILE_LOGIN_FAIL_COUNT).toString();
                if (StringUtil.isNotBlank(countStr)) {
                    count = Integer.parseInt(countStr.trim());
                }
            }
        }
        return count;
    }
}
